package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.dto.mumber.UserLoginDto;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.service.WechatService;
import com.zzyl.nursing.vo.mumber.LoginVo;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 老人家属Service业务层处理
 *
 * @author mr.luo
 * @date 2025-05-23
 */
@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService {
    @Autowired
    private FamilyMemberMapper familyMemberMapper;
    @Autowired
    private WechatService wechatService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询老人家属
     *
     * @param id 老人家属主键
     * @return 老人家属
     */
    @Override
    public FamilyMember selectFamilyMemberById(Long id) {
        return getById(id);
    }

    /**
     * 查询老人家属列表
     *
     * @param familyMember 老人家属
     * @return 老人家属
     */
    @Override
    public List<FamilyMember> selectFamilyMemberList(FamilyMember familyMember) {
        return familyMemberMapper.selectFamilyMemberList(familyMember);
    }

    /**
     * 新增老人家属
     *
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int insertFamilyMember(FamilyMember familyMember) {
        return save(familyMember) ? 1 : 0;
    }

    /**
     * 修改老人家属
     *
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int updateFamilyMember(FamilyMember familyMember) {
        return updateById(familyMember) ? 1 : 0;
    }

    /**
     * 批量删除老人家属
     *
     * @param ids 需要删除的老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除老人家属信息
     *
     * @param id 老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberById(Long id) {
        return removeById(id) ? 1 : 0;
    }


    /**
     * 小程序登录
     *
     * @param dto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public LoginVo login(UserLoginDto dto) {
        //1.先获取用户的openId
        String openId = wechatService.getOpenId(dto.getCode());
        if (StrUtil.isBlank(openId)) {
            throw new BaseException("获取openId失败");
        }
        List<String> list = Arrays.asList("花开富贵", "小花", "小王", "小李", "小张", "小赵",
                "小孙", "小李", "小王", "小赵", "小孙", "小李", "小王", "小赵", "小孙", "小李",
                "小王", "小赵", "小孙", "小李", "小王", "小赵", "小孙", "小李");
        Collections.shuffle(list);
        String nickName = list.get(0) + RandomStringUtils.randomNumeric(4);
        //2. 判断用户，不存在则创建
        FamilyMember one = lambdaQuery()
                .eq(FamilyMember::getOpenId, openId)
                .one();
        if (one == null) {
            one = FamilyMember.builder().name(nickName).openId(openId).build();
            boolean save = save(one);
            if (!save) {
                throw new BaseException("创建用户失败");
            }
        }
        //3. 判断手机号，不存在则根据openId获取用户手机号
        if (StrUtil.isBlank(one.getPhone())) {
            String phone = wechatService.getPhone(dto.getPhoneCode());
            if (StrUtil.isBlank(phone)) {
                throw new BaseException("获取手机号失败");
            }
            //4. 获取到手机号，就更新用户信息
            one.setPhone(phone);
            updateById(one);
        }
        //5. 生成令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", one.getId());
        claims.put("name", one.getName());
        String token = tokenService.createToken(claims);
        //6. 封装数据返回
        LoginVo vo = new LoginVo(token, one.getName());
        return vo;
    }
}
